﻿package ua.mihaylenko.canvas.data 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.getDefinitionByName;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.setTimeout;
	import flash.xml.XMLNode;
	import ua.mihaylenko.canvas.controller.CanvasEvent;
	import ua.mihaylenko.canvas.controller.EventManager;
	import ua.mihaylenko.canvas.common.DocumentClass;
	import ua.mihaylenko.canvas.gui.photoshop.canvas.GUICanvas;
	import ua.mihaylenko.canvas.gui.photoshop.GUIPhotoshop;
	import ua.mihaylenko.canvas.utils.PopUpManager;
	/**
	 * ...
	 * @author Mihayelnko A.L.
	 */
	public dynamic class DataManager extends EventDispatcher
	{
		private static var _cInstance:DataManager;
		public var priceText:String = ""
		public var helpText:String = ""
		
		private var _sUploadURL:String;
		private var _sUploadDir:String;
		private var _bmdPicture:BitmapData;
		private var _bmdFrontImg:BitmapData;
		
		private var _bmdLeftImage:BitmapData;
		
		private var _bmdRightImage:BitmapData;
		
		private var _bmdTopImage:BitmapData;
		
		private var _bmdBottomImage:BitmapData;
		private var _bmdBackImage:BitmapData;
		private var _xPopUp:XML;
		private var _cPopUpColor:uint;
		
		private var _aNameCanvas:Array;
		private var _aNameFronts:Array;
		private var _aNameMedia:Array;
		private var _aNameCorrection:Array;
		private var _arrFrame:Array = new Array();
		private var _arrImage:Array = new Array();
		private var _numPrice:Number;
		private var _sCurrency:String;
		
		
		public var numItems:int = 0;
		public var pathToImage:int;
		public var zoom:int = 1;
		public var rotation:int = 0;
		public var color:int = 0xffffff;
		public var boxWidth:Number;
		public var boxHeight:Number;
		public var boxDepth:Number;
		public var imageX:int = 0;
		public var imageY:int = 0;
		public var filter:int = 1;
		public var sFinishApp:String = "";
		public var fileName:String = "imagename";
		public var filePrevName:String = "";
		private static var xmlQuality:XML;
		
		
		public function DataManager() 
		{
			
		}
		public function set frontImage(source:BitmapData) {
			_bmdFrontImg = source;
		}
		public function get frontImage():BitmapData {
			return _bmdFrontImg;
		}
		public function set leftImage(source:BitmapData) {
			_bmdLeftImage = source;
		}
		public function get leftImage():BitmapData {
			return _bmdLeftImage;
		}
		public function set rightImage(source:BitmapData) {
			_bmdRightImage = source;
		}
		public function get rightImage():BitmapData {
			return _bmdRightImage;
		}
		public function set topImage(source:BitmapData) {
			_bmdTopImage = source;
		}
		public function get topImage():BitmapData {
			return _bmdTopImage;
		}
		public function set bottomImage(source:BitmapData) {
			_bmdBottomImage = source;
		}
		public function get bottomImage():BitmapData {
			return _bmdBottomImage;
		}
		public function set backImage(source:BitmapData) {
			_bmdBackImage = source;
		}
		public function get backImage():BitmapData {
			var cImage:Class = getDefinitionByName("BackImage") as Class;
			return (new cImage(375, 250) as BitmapData);
		}
		public static function getInatsnce():DataManager {
			if (!_cInstance) _cInstance = new DataManager();
			return _cInstance;
		}
		public function set image(img:BitmapData) {
			_bmdPicture = img;
			EventManager.getInstance().dispatchEvent(new CanvasEvent(CanvasEvent.FILE_CHANGE));
			setTimeout(setCanvasImage, 200);
		}
		private function setCanvasImage():void {
			GUIPhotoshop.THIS.init();
			GUICanvas.THIS.image = _bmdPicture;
		}
		public function get image():BitmapData {
			if(_bmdPicture){
				return _bmdPicture.clone();
			}
			return null;
		}
		public function addMediaName(sName:String) { 
			if (!_aNameMedia)_aNameMedia = new Array();
			_aNameMedia.push(sName);
		}
		public function addCorrectionName(sName:String) { 
			if (!_aNameCorrection)_aNameCorrection = new Array();
			_aNameCorrection.push(sName);
		}
		
		public function addCanvasName(sName:String) {
			if (!_aNameCanvas)_aNameCanvas = new Array();
			_aNameCanvas.push(sName);
		}
		public function addFrontName(sName:String) {
			if (!_aNameFronts)_aNameFronts = new Array();
			_aNameFronts.push(sName);
		}
		public function get canvas():Array {
			return _aNameCanvas;
		}
		public function get media():Array {
			return _aNameMedia;
		}
		public function get correction():Array {
			return _aNameCorrection;
		}
		public function get front():Array {
			return _aNameFronts;
		}
		public function get baseURL():String {
			var aURL=new Array();
			var myURL:String = "";
			aURL = DocumentClass.AppStage.loaderInfo.url.split("/");
			for(var count = 0;count<(aURL.length-1);count++){
				myURL +=aURL[count] +"/"
			}
			return myURL;

		}
		public function parseVars(loaderInfo:LoaderInfo) {
			var cRequest:URLRequest = new URLRequest();
			if (loaderInfo.parameters['Config']) {
				cRequest.url = loaderInfo.parameters['Config'];
			}else {
				cRequest.url = GlobalSettings.DEFAULT_DATA_URL;
			}
			var cURLLoader:URLLoader = new URLLoader();
			cURLLoader.addEventListener(Event.COMPLETE, dataLoaded);
			cURLLoader.load(cRequest);
		}
		public function dataLoaded(event:Event) {
			event.target.removeEventListener(Event.COMPLETE, dataLoaded);
			var xXML:XML = XML(event.target.data);
			_sUploadDir = xXML.uploadDir.@url;
			_sUploadURL = xXML.uploadApp.@url;
			sFinishApp = xXML.finishApp.@url;
			_sCurrency = xXML.price.@currency;
			priceText = xXML.window.price;
			helpText = xXML.window.help;
			/**/
			if (xXML.popUpConfig is XMLList) {
				_xPopUp = xXML.popUpConfig[0];
				bgPopUpColor = uint(_xPopUp.@bgColor);
			}
			if (xXML.canvas is XMLList) {
				parseCanvasSize(xXML.canvas[0]);
			}
			this.dispatchEvent(new Event(Event.COMPLETE));
			xmlQuality = new XML();
			xmlQuality = xXML.quality[0];
		}
		public static function getQualityString(value:int):String {
			try { xmlQuality.children().length() } catch (e:*) { return "" };
			//trace(xmlQuality.children().length());
			var j:int = 0;
			for (j = 0; j < xmlQuality.children().length() ; j++) 
			{
			//	trace(xmlQuality.children()[j].@value, value)
				if (int(xmlQuality.children()[j].@value)<=value) 
				{
					return xmlQuality.children()[j].@text;
				}
			}
			return xmlQuality.children()[j-1].text;
		}
		private function parseCanvasSize(xCanvas:XML) {
			for each(var size:XML in xCanvas.size) {
				addFrontName(size.@name);
				_arrImage.push( { w:size.@w, h:size.@h ,price:size.@cost} );
			}
			for each(var frame:XML in xCanvas.frame) {
				addCanvasName(frame.@name);
				_arrFrame.push( { w:frame.@w, price:frame.@cost} );
			}
			
			for each(var media:XML in xCanvas.media) {
				addMediaName(media.@name);
			}
			for each(var correction:XML in xCanvas.correction) {
				addCorrectionName(correction.@name);
			}
			
		}
		
		public function configuratePopUp(_oObject:DisplayObjectContainer) {
			if (_xPopUp && _oObject) {
				for (var i:int = 0; i < _oObject.numChildren; i++ ) {
					if(_oObject.getChildAt(i) is InteractiveObject){
						var hoverObject:InteractiveObject = InteractiveObject(_oObject.getChildAt(i));
						var sPopUp:String = new String(_xPopUp[hoverObject.name].@text);
						if (sPopUp) {
							var cPopUp:PopUpManager = new PopUpManager(sPopUp);
							cPopUp.attach(hoverObject);
							_oObject.addChild(cPopUp);
							cPopUp.visible = false;
						}
					}
				}
			}
		}
		public function set bgPopUpColor(cPopUpColor:uint) {
			_cPopUpColor = cPopUpColor;
		}
		public function get bgPopUpColor():uint {
			return _cPopUpColor;
		}
		public function get uploadDir():String {
			return _sUploadDir;
		}
		
		public function get uploadURL():String {
			return _sUploadURL;
		}
		
		public function get arrFrame():Array { return _arrFrame; }
		
		public function get arrImage():Array { return _arrImage; }
		
		public function get numPrice():Number { return _numPrice; }
		
		public function get sCurrency():String { return _sCurrency; }
	}
	
}